import 'package:chewie/chewie.dart';
import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';

/// https://blog.csdn.net/weixin_38025168/article/details/103921915

class VideoApp extends StatefulWidget {
  @override
  _VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<VideoApp> {
  VideoPlayerController _controller;
  ChewieController chewieController;

  @override
  void initState() {
    super.initState();

    /*_controller = VideoPlayerController.network(
        'https://video.laodao.so/bbs/content/22d7f15d-d47d-4533-90fa-8d80f6a7f36d.mp4')
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });*/

    _controller = VideoPlayerController.asset('assets/video/yourname.mp4')
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Video Demo',
      home: VideoContent(chewieController, _controller),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}

class VideoContent extends StatelessWidget {
  ChewieController chewieController;
  VideoPlayerController _controller;

  VideoContent(this.chewieController, this._controller);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final width = size.width;
    final height = size.height;
    print('_VideoAppState.initState width=$width  height=$height');
    chewieController = ChewieController(
      videoPlayerController: _controller,
      aspectRatio: width / height, //宽高比
      autoPlay: false, //自动播放
      looping: false, //循环播放
    );

    return Scaffold(
      body: getVideoContent(),
      /*floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
              _controller.value.isPlaying
                  ? _controller.pause()
                  : _controller.play();
            });
          },
          child: Icon(
            _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
          ),
        ),*/
    );
  }

  Widget getVideoContent() {
    /* return Center(
      child: _controller.value.initialized
          ? AspectRatio(
              aspectRatio: _controller.value.aspectRatio,
              child: VideoPlayer(_controller),
            )
          : Container(),
    );*/

    return Center(
      child: Chewie(
        controller: chewieController,
      ),
    );
  }
}
